package com.fancy.learncenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.YgtListAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.WDListDataBean;
import com.fancy.learncenter.bean.YgtListDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.fancy.learncenter.view.MyItemDecoration;
import com.fancy.learncenter.view.MyItemTouchCallback;
import com.fancy.learncenter.view.MyXRefreshView;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class YgtDetailActivity extends BaseActivity {

    String classifyId = "";
    @Bind(R.id.recycleView)
    RecyclerView recycleView;


    YgtListAdapter ygtListAdapter;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ygt_detail, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);

        classifyId = getIntent().getStringExtra("classifyId");
        initToolbar("项目");
        setRight("添加项目", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(YgtDetailActivity.this, YGTAddActivity.class);

                intent.putExtra("classifyId", classifyId);

                startActivity(intent);
            }

        });


        initRcycleView();

    }


    @Override
    protected void onResume() {
        super.onResume();
        getData(true);
    }

    private void initRcycleView() {
        ArrayList itemDatas = new ArrayList();
        ygtListAdapter = new YgtListAdapter(this, itemDatas, classifyId);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        recycleView.setItemAnimator(new DefaultItemAnimator());
        recycleView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
//        recycleView.addItemDecoration(new MyItemDecoration(this, MyItemDecoration.VERTICAL_LIST));

        MyItemTouchCallback myItemTouchCallback = new MyItemTouchCallback(ygtListAdapter);
        myItemTouchCallback.setSwipeCallBack(new MyItemTouchCallback.SwipeCallBack() {
            @Override
            public void startSwipe() {
                xRefreshView.disallowInterceptTouchEvent(true);
            }

            @Override
            public void endSwipe() {
                xRefreshView.disallowInterceptTouchEvent(false);
                setOrder();
            }
        });
        ItemTouchHelper helper = new ItemTouchHelper(myItemTouchCallback);
        helper.attachToRecyclerView(recycleView);

        recycleView.setAdapter(ygtListAdapter);


        ygtListAdapter.setCustomLoadMoreView(new CustomFootView(this));
        xRefreshView.setPullLoadEnable(true);

        xRefreshView.setCustomHeaderView(new CustomHeaderView(this));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                LogUtil.MyLog("onLoadMore", "====onRefresh==");
                getData(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getData(false);
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });

    }

    int page = 0;

    private void getData(final boolean isRefresh) {
        HashMap maps = new HashMap();
        maps.put("classifyId", classifyId);

        if (isRefresh) {
            page = 0;
        } else {
            page++;
        }
        maps.put("page", page + "");


        HttpMehtod.getInstance().ygtDetailList(maps, new IdeaObserver<BaseDataBean<List<YgtListDataBean>>>(this, isRefresh) {

            @Override
            public void onSuccess(BaseDataBean<List<YgtListDataBean>> listBaseDataBean) {


                if (isRefresh) {

                    if (xRefreshView == null) {
                        return;
                    }
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);

                    if (listBaseDataBean.getData() == null) {
                        ygtListAdapter.notifyDataSetChanged(new ArrayList<YgtListDataBean>());
                        return;
                    } else {
                        ygtListAdapter.notifyDataSetChanged((ArrayList<YgtListDataBean>) listBaseDataBean.getData());
                    }


                } else {
                    if (listBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }


                    if (listBaseDataBean.getData() == null) {
                        return;
                    }
                    ygtListAdapter.addAll(listBaseDataBean.getData());

                }

            }

            @Override
            public void onFail(int errorCode) {
                xRefreshView.stopRefresh();
                xRefreshView.setLoadComplete(false);
            }

        });
    }

    private void setOrder() {
        HashMap maps = new HashMap();
        maps.put("classifyId", classifyId);
        List<String> orderId = new ArrayList<>();
        for (int i = 0; i < ygtListAdapter.itemDatas.size(); i++) {
            orderId.add(ygtListAdapter.itemDatas.get(i).getId());
        }

        HttpMehtod.getInstance().sygtetSeque(maps, orderId, new IdeaObserver<BaseDataBean>(this, false) {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {


            }

            @Override
            public void onFail(int errorCode) {
            }

        });
    }


    @OnClick({R.id.galary_img, R.id.video_img})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.galary_img:
                intent = new Intent(this, YgtGalleryActivity.class);
                intent.putExtra("classifyId", classifyId);
                startActivity(intent);
                break;
            case R.id.video_img:
                intent = new Intent(this, YgtVideoActivity.class);
                intent.putExtra("classifyId", classifyId);
                startActivity(intent);

                break;
        }
    }
}
